/*
 *
 * Copyright (C) 1997 Jayakrishnan Nair jk@csr.uvic.ca
 * WWW: http://www.csc.uvic.ca/~jk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
import java.awt.*;
import java.applet.Applet;
import java.net.URL;
import java.awt.image.*;
import java.net.*;
import java.io.*;

class ImageLoader extends Canvas implements Runnable
{
    static final int NUM_TRIES = 10;
    static final int BACKOFF_MEAN = 100;
    static final int MAXWAITTIME = 60000;
    MediaTracker tracker;
    Thread thread;
    SlideProjector ss;
    int n;
    String url;


    public ImageLoader(SlideProjector slideProjector, int i, String string)
    {
        ss = slideProjector;
        n = i;
        url = string;
        thread = new Thread(this);
        thread.start();

    }

    public void run()
    {

        thread.setPriority(Math.max(1, 5 - n));
        if (tracker == null)
            tracker = new MediaTracker(ss);
        try
        {
            if (url.startsWith("/") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("gopher://")) {
               ss.images[n] = Toolkit.getDefaultToolkit().getImage(new URL(url));
            }
            else {
                ss.images[n] = Toolkit.getDefaultToolkit().getImage(new URL(ss.getDocumentBase(), url));

            }
        }
        catch (Exception e1)
        {


        }



        for (int i = 0; i < 10; i++)
        {

            try
            {
                tracker.addImage(ss.images[n], n);
                tracker.checkID(n, true);

                tracker.waitForID(n);
                ss.showMessage("[Got image for slide: " + (n + 1)+"]");
                return;
            }
            catch (Exception e2)
            {
                if (!tracker.isErrorID(n))
                    break;
                ss.showMessage("Timeout waiting for "+url);
            }
        }


          System.err.println("Failed to load image for slide: " + (n + 1));
    }


    private  ImageProducer fetchImage(String string) {
    ImageProducer imageProducer = null;
    try {
      imageProducer = (ImageProducer)new URL(string).getContent();
    } catch (MalformedURLException e1) {
      ss.showMessage("dodgy URL. Sort it out, matey."+string);
    } catch (ClassCastException e2){
      ss.showMessage("probably a dodgy URL. Sort it out, matey."+string);
    } catch (IOException e3) {
      ss.showMessage("failed to get image via http."+string);
    }
    return imageProducer;
  }
}
